package com.example.su_shi_tan_app

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSON
import com.example.su_shi_tan_bas_lib.base.BaseActivityEx
import com.example.su_shi_tan_bas_lib.bean.UpDataVersionBean
import com.example.su_shi_tan_core.arouter.AppARouter
import com.example.su_shi_tan_core.arouter.AppARouterParams
import com.example.su_shi_tan_core.arouter.HomeARouter
import com.example.su_shi_tan_core.arouter.MainARouter
import com.example.su_shi_tan_core.db.LogInTokenManager
import com.example.su_shi_tan_core.extentions.castBaseFragmentEx
import com.example.su_shi_tan_core.getUserInfo
import com.example.su_shi_tan_core.utils.HiDataBus
import com.lxj.xpopup.XPopup
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator
import me.yokeyword.fragmentation.anim.FragmentAnimator
import retrofit2.http.Path

@Route(path = AppARouter.Main_Activity)
class MainActivity : BaseActivityEx() {

    private var mVersionBean: AppVersion? = null

    private var mUpDataVersionPop: UpDataVersionPop? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (savedInstanceState == null) {
            intiRootContentView()

            if (LogInTokenManager.tokenManager.haveSignedIn()) {
                getUserInfo(this)
            }

            HiDataBus.with<String>(AppARouterParams.AppUpDataParams)
                .observerSticky(this, false, {
                    if (mUpDataVersionPop != null) {
                        mUpDataVersionPop?.dismiss()
                    }
                    parserAppUpData(it)
                })

        }
    }

    private fun parserAppUpData(it: String?) {
        mVersionBean = JSON.parseObject(it, AppVersion::class.java)
        if (!compareVersion(mVersionBean?.version_number)) {
            mUpDataVersionPop = UpDataVersionPop(this)
            mUpDataVersionPop?.setVersionBean(mVersionBean)
            mUpDataVersionPop?.setTarget(this)
            XPopup.Builder(this)
                .hasShadowBg(true)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(mUpDataVersionPop)
                .show()

        }
//        val version_number = obj["version_number"]
//        val app_download_url = obj["app_download_url"]

    }

    private fun compareVersion(remoteVersion: String?): Boolean {
        if (remoteVersion?.isEmpty() == true) return false
        val localeVersion = BuildConfig.VERSION_NAME
        return localeVersion == remoteVersion
//        var remoteSplit = remoteVersion?.split(".")
//        var localeSplit = localeVersion.split(".")
//         if (remoteSplit)
    }

    private fun intiRootContentView() {
        loadRootFragment(
            R.id.mContainer,
            ARouter.getInstance()
                .build(MainARouter.Main_Fragment)
                .castBaseFragmentEx()
        )
    }

    override fun onCreateFragmentAnimator(): FragmentAnimator {

        // 设置横向(和安卓4.x动画相同)
        return DefaultHorizontalAnimator()
    }
}